package com.chain.BrotherExchange.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.model.MoneyRecordModel;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateFragment;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.DateUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import butterknife.Bind;

public class LockedPositionFragment extends AbsNetworkStateFragment implements OnRefreshLoadmoreListener {

    @Bind(R.id.recycler_view) RecyclerView mRecyclerView;
    @Bind(R.id.smart_refresh_layout) SmartRefreshLayout mSmartRefreshLayout;

    private int mPageType;
    private int currentPage = 1;
    private LockedAdapter mAdapter;


    // 锁仓明细
    public static Fragment newLockedPosition(){
        return LockedPositionFragment.newInstance(20);
    }

    // 释放明细
    public static Fragment newRelease(){
        return LockedPositionFragment.newInstance(24);
    }

    // 解锁明细
    public static Fragment newUnlock(){
        return LockedPositionFragment.newInstance(22);
    }

    // 推广奖励
    public static Fragment newGeneralize(){
        return LockedPositionFragment.newInstance(26);
    }

    // 节点奖励
    public static Fragment newNode(){
        return LockedPositionFragment.newInstance(27);
    }

    private static Fragment newInstance(int type){
        Fragment fragment = new LockedPositionFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(C.EXTRA_PAGE_TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.simple_load_refresh_recycler_view;
    }

    @Override
    protected void initActions() {
        Bundle arguments = getArguments();
        mPageType = arguments.getInt(C.EXTRA_PAGE_TYPE);

        // init view
        mSmartRefreshLayout.setOnRefreshLoadmoreListener(this);
        mAdapter = new LockedAdapter();
        mRecyclerView.setAdapter(mAdapter);

        // 请求数据
        initLockedPositionData();
    }

    private void initLockedPositionData(){
        currentPage = 1;
        loadingNetwork();
        int uid = Androids.LocalConfig.getUid();
        String token = Androids.LocalConfig.getToken();

        DataRepository.get().getMoneyRecord(uid, token, currentPage, mPageType, result -> {
            mSmartRefreshLayout.finishRefresh();

             if (result != null && result.isSucceed()){
                List<MoneyRecordModel.DataBean.ListBean.ItemsBean> items = result.getData()
                        .getList().getItems();
                if(!items.isEmpty()){
                    successNetwork();
                    mAdapter.setData(items);
                } else {
                    emptyNetwork();
                }
            } else if(result != null){
                errorNetwork(result.getMsg());
            }
        });
    }

    private void loadMoreLockedPositionData(){
        int uid = Androids.LocalConfig.getUid();
        String token = Androids.LocalConfig.getToken();

        DataRepository.get().getMoneyRecord(uid, token, ++currentPage, mPageType, result -> {
            mSmartRefreshLayout.finishLoadmore();
            if (result != null && result.isSucceed()){
                List<MoneyRecordModel.DataBean.ListBean.ItemsBean> items = result.getData()
                        .getList().getItems();
                if(!items.isEmpty()){
                    successNetwork();
                    mAdapter.addData(items);
                }
            }
        });
    }

    @Override
    public void onAnewRequest() {
        initLockedPositionData();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        loadMoreLockedPositionData();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        initLockedPositionData();
    }


    static class LockedAdapter extends RecyclerView.Adapter<LockedAdapter.ViewHolder> {

        private List<MoneyRecordModel.DataBean.ListBean.ItemsBean> list;

        @NonNull
        @Override
        public LockedAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            return new ViewHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_locked_position, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull LockedAdapter.ViewHolder holder, int i) {
            MoneyRecordModel.DataBean.ListBean.ItemsBean itemsBean = list.get(i);
            holder.mDateText.setText(DateUtils.timestampToTime(itemsBean.getCreated_time()));
            holder.mNumberText.setText(itemsBean.getNum());
        }

        @Override
        public int getItemCount() {
            return list != null ? list.size() : 0;
        }

        public void setData(List<MoneyRecordModel.DataBean.ListBean.ItemsBean> list){
            this.list = list;
            notifyDataSetChanged();
        }

        public void addData(List<MoneyRecordModel.DataBean.ListBean.ItemsBean> items){
            this.list.addAll(items);
            notifyDataSetChanged();
        }

        static class ViewHolder extends RecyclerView.ViewHolder{

            private TextView mNumberText, mDateText;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                mNumberText = itemView.findViewById(R.id.number_text);
                mDateText = itemView.findViewById(R.id.date_text);
            }
        }
    }
}
